<template>
  <div>
    <div class="list-search">
      <ul class="search-list">
        <li style="float:left; margin-right:auto;">
          <el-button
            type="success"
            icon="el-icon-plus"
            plain
            @click="showDialog('add')"
            >添加社区信息</el-button
          >
        </li>
        <li>
          <el-input
            v-model="searchList.name"
            placeholder="请输入社区名称"
          ></el-input>
        </li>
        <li>
          <el-input
            v-model="searchList.num"
            placeholder="请输入社区编号"
          ></el-input>
        </li>
        <li>
          <el-date-picker
            v-model="searchList.selectTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </li>
        <li>
          <el-button
            type="primary"
            icon="el-icon-search"
            plain
            @click="search()"
            >查询</el-button
          >
        </li>
      </ul>
    </div>
    <el-table :data="list" border v-loading="loading" style="width: 100%">
      <el-table-column prop="num" label="社区编号"> </el-table-column>
      <el-table-column prop="name" label="社区名称"> </el-table-column>
      <el-table-column prop="duty" label="负责人"> </el-table-column>
      <el-table-column prop="house" label="社区家庭数量"> </el-table-column>
      <el-table-column prop="person" label="社区人口数量"> </el-table-column>
      <el-table-column prop="created" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="showDialog('modify', scope.row, scope.$index)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      width="480px"
    >
      <el-form :model="form" :label-width="formLabelWidth">
        <el-form-item v-if="dialogType == 'modify'" label="社区编号">
          <el-input disabled v-model="form.num"></el-input>
        </el-form-item>
        <el-form-item label="社区名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="社区负责人">
          <el-input v-model="form.duty"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogType == 'modify'" type="primary" @click="modify"
          >确 定</el-button
        >
        <el-button v-if="dialogType == 'add'" type="primary" @click="add"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { storage } from "../../utils/storage";
import { utils } from "../../utils/common.js";
export default {
  data() {
    return {
      list: [],
      houseList: [],
      loading: false,
      searchList: {
        name: "",
        num: "",
        selectTime: null
      },
      houseList: storage.getStorage("house"),
      personList: storage.getStorage("person"),
      communityIndex: null,
      form: {},
      dialogFormVisible: false,
      dialogTitle: "添加社区信息",
      dialogType: "add",
      formLabelWidth: "120px",
      // dialogIndex: null,
      total: 0,
      pageNum: 1,
      pageSize: 5
    };
  },
  methods: {
    getTimes(str) {
      return new Date(str).getTime();
    },
    getList(n, isSearch) {
      this.loading = true;
      var cloneList = storage.getStorage("community");
      if (isSearch) {
        if (this.searchList.name != "") {
          cloneList = cloneList.filter(
            item => item.name == this.searchList.name
          );
        }
        if (this.searchList.num != "") {
          cloneList = cloneList.filter(item => item.num == this.searchList.num);
        }
        if (this.searchList.selectTime != null) {
          cloneList = cloneList.filter(
            item =>
              this.getTimes(item.created) >=
                this.getTimes(this.searchList.selectTime[0]) &&
              this.getTimes(item.created) <=
                this.getTimes(this.searchList.selectTime[1])
          );
        }
      }
      var resList =
        cloneList.length - (n - 1) * this.pageSize >= this.pageSize
          ? cloneList.slice((n - 1) * this.pageSize, this.pageSize * n)
          : cloneList.slice((n - 1) * this.pageSize);
      for (let i = 0, j; i < resList.length; i++) {
        j = this.houseList.filter(item => item.community == resList[i].id)
          .length;
        resList[i].house = j;
        if (j == 0) {
          resList[i].person = j;
        } else {
          resList[i].person = this.personList.filter(
            item => item.community == resList[i].id
          ).length;
        }
      }
      setTimeout(() => {
        this.list = resList;
        this.total = cloneList.length;
        this.pageNum = n;
        this.loading = false;
      }, 500);
    },
    search() {
      if (
        this.searchList.name == "" &&
        this.searchList.num == "" &&
        this.searchList.selectTime == null
      ) {
        return this.getList(1);
      }
      this.getList(1, true);
    },
    showDialog(type, temp) {
      this.dialogFormVisible = true;
      this.dialogType = type;
      if (type == "add") {
        this.dialogTitle = "添加社区信息";
        this.form = {};
      } else {
        this.dialogTitle = "修改社区信息";
        this.form = JSON.parse(JSON.stringify(temp));
        var arr = storage.getStorage("community");
        var n;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == this.form.id) {
            n = i;
            break;
          }
        }
        this.communityIndex = n;
      }
    },
    modify() {
      setTimeout(() => {
        var arr = storage.getStorage("community");
        arr[this.communityIndex] = this.form;
        storage.setStorage("community", arr);
        this.dialogFormVisible = false;
        this.getList(1);
        this.$message.success("修改成功");
      }, 500);
    },
    add() {
      if (this.form.name == "" || this.form.duty == "") {
        return this.$message.warning("请将信息填写完整");
      }
      var arr = storage.getStorage("community");
      this.form.id = this.form.num = arr[arr.length - 1].id + 1;
      this.form.created = utils.formatDate(new Date().getTime());
      arr.push(this.form);
      storage.setStorage("community", arr);
      this.dialogFormVisible = false;
      this.$message.success("添加成功");
      this.getList(1);
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      if (
        this.searchList.name == "" &&
        this.searchList.num == "" &&
        this.searchList.selectTime == null
      ) {
        return this.getList(e);
      } else {
        this.getList(e, true);
      }
    }
  },
  mounted() {
    this.getList(1);
  }
};
</script>

<style scoped>
.el-input {
  width: 200px;
}
</style>
